home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource4 / 221_01 / cc31.c < prev    next >
Text File  |  1979-12-31  |  8KB  |  396 lines

  1. /* >>>>> start of cc3 <<<<<<<<< */
  2. /*     */
  3. /* Perform a function call  */
  4. /*     */
  5. /* called from heir11, this routine will either call */
  6. /* the named function, or if the supplied ptr is */
  7. /* zero, will call the contents of HL  */
  8. callfunction(ptr)
  9.  char *ptr; /* symbol table entry (or 0) */
  10. { int nargs;
  11.  nargs=0;
  12.  blanks(); /* already saw open paren */
  13.  if(ptr==0)push(); /* calling HL */
  14.  while(streq(line+lptr,")")==0)
  15.   {if(endst())break;
  16.   expression(); /* get an argument */
  17.   if(ptr==0)swapstk(); /* don't push addr */
  18.   push(); /* push argument */
  19.   nargs=nargs+2; /* count args*2 */
  20.   if (match(",")==0) break;
  21.   }
  22.  needbrack(")");
  23.  if(streq(ptr,"printf")) {
  24.    immed();     /* argument counter in primary register */
  25.    outdec(nargs>>1);     /*  for printf etc. */
  26.    nl();                 /*  Dieter H. Flunkert     20-jul-86  */
  27.  }
  28.  if(ptr)call(ptr);
  29.  else callstk();
  30.  stkp=modstk(stkp+nargs); /* clean up arguments */
  31. }
  32. junk()
  33. { if(an(inbyte()))
  34.   while(an(ch()))gch();
  35.  else while(an(ch())==0)
  36.   {if(ch()==0)break;
  37.   gch();
  38.   }
  39.  blanks();
  40. }
  41. endst()
  42. { blanks();
  43.  return ((streq(line+lptr,";")|(ch()==0)));
  44. }
  45. illname()
  46. { errrpt("illegal symbol name");junk();}
  47. multidef(sname)
  48.  char *sname;
  49. { errrpt("already defined");
  50.  comment();
  51.  outstr(sname);nl();
  52. }
  53. needbrack(str)
  54.  char *str;
  55. { if (match(str)==0)
  56.   {errrpt("missing token");
  57.   comment();outstr(str);nl();
  58.   }
  59. }
  60. needlval()
  61. { errrpt("must be lvalue");
  62. }
  63.  
  64. hash(sname) char *sname; {
  65.   int h, c;
  66.   h = *sname;
  67.   while(c = *(++sname)) h=(h<<1)+c;
  68.   return h;
  69. }
  70.  
  71. /*
  72. ** findstatic variable
  73. ** dhf 29-oct-86
  74. */
  75. findstat(sname) char *sname; {
  76. char *ptr;
  77.   ptr=statptr;
  78.   while(ptr >= startstat) {
  79.    ptr = ptr - statsize;
  80.    if(astreq(sname, ptr, namemax)) return ptr;
  81.   }
  82.   return 0;
  83. }
  84.  
  85. findglb(sname) char *sname; {
  86.  int h;
  87.  h=hash(sname)&MASKGLBS;
  88.  cptr=startglb+h*symsiz;
  89.  while(astreq(sname,cptr,namemax)==0) {
  90.    if(*cptr==0) return 0;
  91.    cptr=cptr+symsiz;
  92.    if(cptr>=endglb) cptr=startglb;
  93.  }
  94.  return cptr;
  95. }
  96. /*
  97. ** find local changed dhf 4-nov-86
  98. ** search is now from end to start
  99. ** of local pointer storage area
  100. */
  101. findloc(sname)
  102.  char *sname;
  103. { char *ptr;
  104.  ptr=locptr;
  105.  while(ptr > endsearch)
  106.   {
  107.    ptr=ptr-symsiz;
  108.    if(astreq(sname,ptr,namemax))return (ptr);
  109.   }
  110.  return (0);
  111. }
  112.  
  113. /*
  114. ** addstatic variable
  115. ** dhf 29-oct-86
  116. */
  117. addstatic(sname, numlab, id, typ, value)
  118. char *sname, *numlab, id, typ;
  119. int value;
  120. {
  121.   char *ptr;
  122.  
  123.   endsearch=startcomp;
  124.   if(findloc(sname)) return statptr;
  125.   endsearch=startloc;
  126.   if(statptr >= endstat) {
  127.    errrpt("static symbol table overflow");
  128.    return 0;
  129.   }
  130.   ptr=statptr;
  131.   while(an(*ptr++ = *sname++)) ; /* copy name */
  132.   ptr=statptr+symsiz;
  133.   while(an(*ptr++ = *numlab++)) ;  /* copy local label */
  134.   statptr[ident] = id;
  135.   statptr[type] = typ;
  136.   statptr[storage] = statik;
  137.   putint(value, statptr+offset, offsize);
  138.   statptr = statptr + statsize;
  139.   return statptr;
  140. }
  141.  
  142. addglb(sname,id,typ,value,class)
  143.  char *sname,id,typ;
  144.  int value,class;
  145. { char *ptr;
  146.  if(findglb(sname))return (cptr);
  147.  if(glbptr>=endglb)
  148.   {errrpt("global symbol table overflow");
  149.   return (0);
  150.   }
  151.  ptr=cptr;
  152.  while(an(*ptr++ = *sname++)); /* copy name */
  153.  cptr[ident]=id;
  154.  cptr[type]=typ;
  155.  cptr[storage]=class;
  156.  putint(value, cptr+offset, offsize);
  157.  glbptr=glbptr+symsiz;
  158.  return (cptr);
  159. }
  160.  
  161. addloc(sname,id,typ,value)
  162.  char *sname,id,typ;
  163.  int value;
  164. { char *ptr;
  165.   endsearch=startcomp;
  166.   if(findloc(sname)) return statptr;
  167.   endsearch=startloc;
  168.  if(locptr>=endloc)
  169.   {errrpt("local symbol table overflow");
  170.   return (0);
  171.   }
  172.  cptr=ptr=locptr;
  173.  while(an(*ptr++ = *sname++)); /* copy name */
  174. /* next lines added for 6809 from dieter flunkert 22 jan 1986 */
  175. /* value has to be adjusted for cchar type varibles. 6809 handles */
  176. /* the values on the stack high/low not /low/high like the 8080 and z80 */
  177.  if((id==variable)&&(typ==cchar)) --value;
  178. /* end of modification 22 jan 1986 */
  179.  cptr[ident]=id;
  180.  cptr[type]=typ;
  181.  cptr[storage]=stkloc;
  182.  putint(value, cptr+offset, offsize);
  183.  locptr=locptr+symsiz;
  184.  return (cptr);
  185. }
  186. #ifdef STGOTO
  187. #ifndef HASH
  188. nextsym(entry) char *entry; {
  189.   entry = entry + name;
  190.   while(*entry++ >= ' ');  /* find length byte */
  191.   return entry;
  192. }
  193. #endif
  194. #endif
  195.  
  196. /*
  197. **  get integer of length len from address addr
  198. **  (byte sequence set by "putint"
  199. */
  200. getint(addr,len) char *addr; int len; {
  201.   int i;
  202.   i = *(addr + --len);  /* high order byte sign extend */
  203.   while(len--) i = (i << 8) | *(addr+len)&255;
  204.   return(i);
  205. }
  206.  
  207. /*
  208. **  put integer i of length len into address addr
  209. **  (low byte first)
  210. */
  211. putint(i, addr, len) char *addr; int i, len; {
  212.   while(len--) {
  213.     *addr++ = i;
  214.     i = i>>8;
  215.     }
  216.   }
  217.  
  218.  
  219. /* Test if next input string is legal symbol name */
  220. symname(sname)
  221.  char *sname;
  222. {
  223.  blanks();
  224.  if(alpha(ch())==0) return (0);
  225.  while(an(ch()))*sname++=gch();
  226.  *sname=0;
  227.  return (1);
  228.  }
  229. /* Return next avail internal label number */
  230. getlabel()
  231. { return(++nxtlab);
  232. }
  233.  
  234. /*
  235. **  print label with newline
  236. */
  237. postlabel(label) int label; {
  238.   printlabel(label);
  239.   nl();
  240. }
  241.  
  242. /* Print specified number as label */
  243. printlabel(label)
  244.  int label;
  245. { outstr("cc");
  246.  outdec(label);
  247. }
  248. /* Test if given character is alpha */
  249. alpha(c)
  250.  char c;
  251. { c=c&127;
  252.  if(c>='a') return(c<='z');
  253.  if(c<='Z') return(c>='A');
  254.  return(c=='_');
  255. }
  256. /* Test if given character is numeric */
  257. numeric(c)
  258.  char c;
  259. { c=c&127;
  260.  if(c>='0') return(c<='9');
  261.  return 0;
  262. }
  263. /* Test if given character is alphanumeric */
  264. an(c)
  265.  char c;
  266. { if(alpha(c)) return 1;
  267.   return(numeric(c));
  268. }
  269. /* Print a new line and a string only to console */
  270. pl(str)
  271.  char *str;
  272. {
  273.  cnl();
  274.  while(*str)putchar(*str++);
  275. }
  276. addwhile(ptr)
  277.  int ptr[];
  278.  {
  279.  int k;
  280.  ptr[wqsp]=stkp;  /* and stk ptr */
  281.  ptr[wqloop]=getlabel(); /* and looping label */
  282.  ptr[wqlab]=getlabel(); /* and exit label */
  283.  if (wqptr==wqmax)
  284.   {errrpt("too many active whiles");exit(ERRCODE);}
  285.  k=0;
  286.  while (k<wqsiz)
  287.   {*wqptr++ = ptr[k++];}
  288. }
  289. delwhile()
  290.  {if(readwhile()) wqptr=wqptr-wqsiz;
  291.  }
  292. readwhile()
  293.  {
  294.  if (wqptr==wq){errrpt("no active whiles");return (0);}
  295.  else return (wqptr-wqsiz);
  296.  }
  297. ch()
  298. { return(line[lptr]&127);
  299. }
  300. nch()
  301. { if(ch())return(line[lptr+1]&127);
  302.   return 0;
  303. }
  304. gch()
  305. { if(ch()) return(line[lptr++]&127);
  306.   return 0;
  307. }
  308. kill()
  309. { lptr=0;
  310.  line[lptr]=0;
  311. }
  312. inbyte()
  313. {
  314.  while(ch()==0)
  315.   {inline();
  316.   if (eof) return (0);
  317.   preprocess();
  318.   }
  319.  return (gch());
  320. }
  321. inchar()
  322. {
  323.  if(ch()==0)inline();
  324.  if(eof)return (0);
  325.  return(gch());
  326. }
  327. inline()
  328.  {
  329.  int k,unit;
  330.  while(1)
  331.   {if (input==0)
  332. #ifdef CMD_LINE
  333.     eof=YES;
  334. #else
  335.     openin();
  336. #endif
  337.   if(eof)return;
  338.   if(CCPOLL())  /* allow program interruption D.H.Flunkert 04-may-86 */
  339.    if(getc(stderr)==3) exit(0);
  340.   if((unit=input2)==0)unit=input;
  341.   kill();
  342.   while((k=getc(unit))>0)
  343.    {if((k==EOL)||(lptr>=linemax))break;
  344.    line[lptr++]=k;
  345.    }
  346.   line[lptr]=0; /* append null */
  347.   if(k<=0)
  348.    {fclose(unit);
  349.    if(input2)input2=0;
  350.     else input=0;
  351.    }
  352.   if(lptr)
  353.    {if((ctext)&&(cmode))
  354.     {cout(';',output);
  355.     sout(line,output);
  356.     cout('\n',output);
  357.     }
  358.    lptr=0;
  359.    if(match("#ifdef")) {
  360.     ++iflevel;
  361.     if(skiplevel) continue;
  362.     blanks();
  363.     if(findmac(&line[lptr])==0)
  364.      skiplevel=iflevel;
  365.     continue;
  366.     }
  367.    if(match("#ifndef")) {
  368.     ++iflevel;
  369.     if(skiplevel) continue;
  370.     blanks();
  371.     if(findmac(&line[lptr]))
  372.      skiplevel=iflevel;
  373.     continue;
  374.     }
  375.    if(match("#else")) {
  376.     if(iflevel) {
  377.      if(skiplevel==iflevel) skiplevel=0;
  378.      else if(skiplevel==0) skiplevel=iflevel;
  379.      }
  380.     else errrpt("no matching #if...");
  381.     continue;
  382.     }
  383.    if(match("#endif")) {
  384.     if(iflevel) {
  385.      if(skiplevel==iflevel) skiplevel=0;
  386.      --iflevel;
  387.      }
  388.     else errrpt("no matching #if...");
  389.     continue;
  390.     }
  391.    if(skiplevel) continue;
  392.    break;
  393.    }
  394.   }
  395. }
  396.